package com.itheima.controller;

import com.itheima.Pojo.Emp;
import com.itheima.Pojo.EmpQueryParam;
import com.itheima.Pojo.PageBean;
import com.itheima.Pojo.Result;
import com.itheima.Service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpControl {
    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * @param param
     * @return 封装好的对象
     */
//    @GetMapping("/emps")
//    public Result page(@RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10") Integer pageSize){
//        log.info("分页查询：{},{}",page,pageSize);
//        PageBean pageBean = empService.page(page,pageSize);
//        return Result.success(pageBean);
//    }
    @GetMapping
    public Result page(EmpQueryParam param) {
        log.info("分页查询：{},{},{},{},{},{}",param.getName(),param.getGender(),param.getBegin(),param.getEnd(),param.getPage(),param.getPageSize());
        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }

    /**
     *
     * @param emp
     * @return 无
     */

    @PostMapping
    public Result add(@RequestBody Emp emp){
        log.info("新增员工:{}",emp);
        empService.add(emp);
        return Result.success();
    }

    /**
     * 删除员工
     * @param ids
     * @return 无
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("根据id删除员工数据：{}",ids);
        empService.delete(ids);
        return Result.success();
    }


    /**
     * 员工回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("回显员工，id={}",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工信息：{}",emp);
        empService.update(emp);
        return Result.success();
    }
}
